/**
 * 
 */
package wangjg.xuexiban.activity.exam;

import wangjg.game.android.AndroidCanvas;
import wangjg.game.layer.Graphics;
import wangjg.game.layer.LayerManager;
import wangjg.game.widget.ImageBox;
import wangjg.xuexiban.R;
import wangjg.xuexiban.activity.exam.layer.BackbroundLayer;
import wangjg.xuexiban.activity.exam.layer.CellBox;
import wangjg.xuexiban.activity.exam.layer.FrameBox;
import wangjg.xuexiban.activity.exam.layer.FrameLayer;
import wangjg.xuexiban.activity.exam.layer.LabelBox;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

/**
 * @author wangjg
 *
 */
@SuppressLint("ClickableViewAccessibility")
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, OnClickListener, OnTouchListener {
	private AndroidCanvas androidCanvas;
	private LayerManager lm;
	
	public MySurfaceView(Context context) {
		super(context);
		this.getHolder().addCallback(this);
		this.setOnTouchListener(this);
		this.setOnClickListener(this);
	}
	
	public void initGame(){
		if(lm!=null){
			return;
		}
		
		int width = this.getWidth();
		int height = this.getHeight();
		int spacing = (int) (width*0.01);
		
		lm = new LayerManager();
		BackbroundLayer bl = new BackbroundLayer(width, height);
		lm.append(bl);
		
		FrameLayer frameLayer = new FrameLayer(width, height, spacing);
		FrameBox barbox = new FrameBox(0.98f, 0.2f, spacing);
		
		Bitmap playIcon = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.play);
		ImageBox playButton = new ImageBox(0.25f, 0.8f);
		playButton.setBitmap(playIcon);
		barbox.addChild(playButton);

		Bitmap stopIcon = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.stop);
		ImageBox stopButton = new ImageBox(0.25f, 0.8f);
		stopButton.setBitmap(stopIcon);
		stopButton.setVisible(2);
		barbox.addChild(stopButton);
		
		LabelBox label = new LabelBox(0.4f, 0.8f);
		label.setText("你好！");
		barbox.addChild(label);
		
		FrameBox mainbox = new FrameBox(0.98f, 0.78f, spacing);
		frameLayer.addChild(barbox);
		frameLayer.addChild(mainbox);
		
		CellBox cl1 = new CellBox(0.3f, 0.2f);
		CellBox cl2 = new CellBox(0.4f, 0.2f);
		CellBox cl3 = new CellBox(0.3f, 0.2f);
		
		mainbox.addChild(cl1);
		mainbox.addChild(cl2);
		mainbox.addChild(cl3);
		
		lm.append(frameLayer);
	}

	public void repaint(){
		Graphics g = androidCanvas.getGraphics();
		lm.paint(g, 0, 0);
		androidCanvas.flushGraphics();
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Log.d("surfaceCreated", "surfaceCreated");
		androidCanvas = new AndroidCanvas(holder);
		this.initGame();
		Canvas canvas = holder.lockCanvas();
		canvas.drawColor(0xff009900);
		holder.unlockCanvasAndPost(canvas);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		Log.d("surfaceChanged", "surfaceChanged");
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d("surfaceDestroyed", "surfaceDestroyed");
		
	}

	@Override
	public void onClick(View v) {
		Log.d("click", ""+v);
		this.repaint();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		Log.d("onTouch", "v="+v+",event="+event);
		int action = event.getAction();
        if(action==MotionEvent.ACTION_UP){
        	v.performClick();
        	return false;
        }  
		return true;
	}

}
